import 'package:pornhub_app/components/easy_toast.dart';
import 'package:pornhub_app/routes/routes.dart';
import 'package:pornhub_app/services/user_service.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import '../../../utils/color.dart';
import 'controller/mine_index_controller.dart';

///每日签到
class DailySignIn extends StatefulWidget {
  final MineIndexController indexcontroller;
  final UserService service;

  const DailySignIn(this.indexcontroller, this.service, {super.key});

  @override
  State<StatefulWidget> createState() => _DailySignIn();
}

class _DailySignIn extends State<DailySignIn> {
  void clickSign() {
    if (!mounted) return;
    setState(() {
      widget.indexcontroller.checkDayNo++;
      widget.indexcontroller.isCheckIn = true;
    });
  }

  @override
  void dispose() {
    Get.delete<MineIndexController>();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.all(10.w),
      decoration: BoxDecoration(
        color: COLOR.hexColor('#ffffff'),
        borderRadius: BorderRadius.circular(6.0),
      ),
      width: double.maxFinite,
      height: 160.w,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisSize: MainAxisSize.min,
        children: [
          Flex(
            direction: Axis.horizontal,
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              Expanded(
                flex: 1,
                child: Flex(
                  direction: Axis.horizontal,
                  children: [
                    Text(
                      '每日签到',
                      style: TextStyle(
                        fontSize: 16.sp,
                        color: Colors.white,
                      ),
                    ),
                    SizedBox(width: 5.w),
                    Text(
                      '签到送金币',
                      style: TextStyle(
                        fontSize: 12.sp,
                        color: Colors.white60,
                      ),
                    ),
                  ],
                ),
              ),
              ButtonWithImage(widget.indexcontroller, widget.service),
            ],
          ),
          const Padding(padding: EdgeInsets.only(top: 15)),
          Expanded(
              child: SiginInfoData(widget.indexcontroller, widget.service)),
        ],
      ),
    );
  }
}

class ButtonWithImage extends StatelessWidget {
  final MineIndexController indexcontroller;
  final UserService service;

  const ButtonWithImage(this.indexcontroller, this.service, {super.key});

  @override
  Widget build(BuildContext context) {
    String buttonImg = service.user.loginStatus == 2
        ? "assets/mine/img_mine_signin.png"
        : indexcontroller.isCheckIn
            ? "assets/mine/img_mine_signin_yes.png"
            : "assets/mine/img_mine_signin.png";

    void onClick() {
      if (service.user.loginStatus == 2) {
        Get.toNamed(Routes.loginregist);
        return;
      }
      if (!indexcontroller.isCheckIn) {
        _DailySignIn? state = context.findAncestorStateOfType<_DailySignIn>();
        state?.clickSign();
        EasyToast.show('签到成功');
        indexcontroller.postCheckIn();
      }
    }

    return InkWell(
      onTap: () => onClick(),
      child: Image(
        image: AssetImage(buttonImg),
        width: 78.w,
        height: 30.w,
      ),
    );
  }
}

class SiginInfoData extends StatelessWidget {
  final MineIndexController indexcontroller;
  final UserService service;

  const SiginInfoData(this.indexcontroller, this.service, {super.key});

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.maxFinite,
      height: double.maxFinite,
      child: CustomScrollView(
        scrollDirection: Axis.horizontal,
        slivers: <Widget>[
          SliverList(
            delegate: SliverChildBuilderDelegate(
              (context, index) {
                return _buildImgText(indexcontroller, index);
              },
              childCount: 7,
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildImgText(MineIndexController indexcontroller, int index) {
    String colorImg = service.user.loginStatus == 2
        ? "1affffff"
        : index < indexcontroller.checkDayNo
            ? "#eb29c6"
            : "#1affffff";
    String colorGoldText = service.user.loginStatus == 2
        ? "#e69d1a"
        : index < indexcontroller.checkDayNo
            ? "#ffffff"
            : "#e69d1a";
    String colorDayText = service.user.loginStatus == 2
        ? "#ccffffff"
        : index < indexcontroller.checkDayNo
            ? "#eb29c6"
            : "#ccffffff";
    String imgSource = service.user.loginStatus == 2
        ? "assets/mine/img_mine_sigin_tips.png"
        : index < indexcontroller.checkDayNo
            ? "assets/mine/img_mine_sigin_tiips_yes.png"
            : "assets/mine/img_mine_sigin_tips.png";

    return Column(mainAxisSize: MainAxisSize.min, children: [
      Container(
        width: 40.w,
        height: 64.w,
        margin: EdgeInsets.only(right: 5.w),
        decoration: BoxDecoration(
          color: COLOR.hexColor(colorImg),
          borderRadius: BorderRadius.circular(8),
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisSize: MainAxisSize.min,
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Image.asset(
              imgSource,
              width: 22.w,
              height: 22.w,
            ),
            Padding(padding: EdgeInsets.only(top: 3.w)),
            Text(
              "${index < 6 ? 1 : 3}金币",
              style: TextStyle(
                fontSize: 12.sp,
                color: COLOR.hexColor(colorGoldText),
              ),
            ),
          ],
        ),
      ),
      Padding(padding: EdgeInsets.only(top: 5.w)),
      Text(
        "第${index + 1}天",
        style: TextStyle(
          fontSize: 12.0,
          color: COLOR.hexColor(colorDayText),
        ),
      ),
    ]);
  }
}
